package com.bingbing.back.service.Impl;

import com.bingbing.back.dto.MajorInfoDto;
import com.bingbing.back.mapper.MajorInfoMapper;
import com.bingbing.back.mapper.SchoolInfoMapper;
import com.bingbing.back.po.MajorInfo;
import com.bingbing.back.service.MajorInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MajorInfoServiceImpl implements MajorInfoService {

    @Resource
    private MajorInfoMapper majorInfoMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;
    private static Map<Integer, Integer> haveId = new HashMap<>();
    private static int batchUpdateSize = 200;

    @Override
    public List<MajorInfo> showAll(MajorInfoDto dto) {
        return majorInfoMapper.search(dto);
    }

    @Override
    public PageInfo<MajorInfo> search(int pageNum, int pageSize, MajorInfoDto dto) {
        PageHelper.startPage(pageNum, pageSize);
        List<MajorInfo> list = majorInfoMapper.search(dto);
        list.forEach(s->{
            addClout(s.getDataId(), 3);
        });
        PageInfo<MajorInfo> page = new PageInfo<>(list, 10);
        return page;
    }

    @Override
    public List<MajorInfoDto> searchByLevel(String majorLevel) {
        return majorInfoMapper.searchByLevel(majorLevel);
    }

    @Override
    public List<MajorInfoDto> searchByClassName(String className) {
        return majorInfoMapper.searchByClassName(className);
    }

    @Override
    public List<MajorInfo> recommendMajor() {
        return majorInfoMapper.recommendMajor();
    }

    @Override
    public MajorInfoDto showMajorById(int dataId) {
        addClout(dataId, 50);
        MajorInfoDto majorInfoDto = majorInfoMapper.showMajorById(dataId);
        String breifStr = majorInfoDto.getBriefIntro();
        String detailStr = majorInfoDto.getDetailIntro();

        String[] breifArray = breifStr.split("\\n");
        String[] detailArray = detailStr.split("\\n");
        majorInfoDto.setBriefIntros(breifArray);
        majorInfoDto.setDetailIntros(detailArray);
        return majorInfoDto;
    }

    @Override
    public void addClout(int dataId, int val) {
        haveId.put(dataId, haveId.getOrDefault(dataId, 0) + val);

        if(haveId.size() >= batchUpdateSize) {
            SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
            try
            {
                MajorInfoMapper Mapper = sqlSession.getMapper(MajorInfoMapper.class);
                Mapper.addClout(haveId);
                sqlSession.commit();
                haveId.clear();
            } catch (Exception e){
                System.out.println(e);
                sqlSession.rollback();
            } finally {
                if(sqlSession != null){
                    sqlSession.close();
                }
            }
        }
    }
}
